/*
 * Copyright 2009 XueSong Guo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.webwheel.utils.fileupload;

import cn.webwheel.Main;
import cn.webwheel.WebContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

class WebContextWrapper implements WebContext {

    private WebContext ctx;
    private Map<String, List<String>> params;

    public WebContextWrapper(WebContext ctx, Map<String, List<String>> params) {
        this.ctx = ctx;
        this.params = params;
    }

    public String getPath() {
        return ctx.getPath();
    }

    public Main getMain() {
        return ctx.getMain();
    }

    public ServletContext getContext() {
        return ctx.getContext();
    }

    public HttpServletRequest getRequest() {
        return new HttpServletRequestWrapper(ctx.getRequest()) {
            @Override
            public String getParameter(String s) {
                List<String> list = params.get(s);
                if(list==null) return null;
                return list.get(0);
            }

            @Override
            public Map getParameterMap() {
                Map<String, String[]> map = new HashMap<String, String[]>(params.size());
                for (Map.Entry<String, List<String>> entry : params.entrySet()) {
                    map.put(entry.getKey(), entry.getValue().toArray(new String[entry.getValue().size()]));
                }
                return map;
            }

            @Override
            public Enumeration getParameterNames() {
                return new Enumeration() {

                    Iterator<Map.Entry<String, List<String>>> it = params.entrySet().iterator();

                    public boolean hasMoreElements() {
                        return it.hasNext();
                    }

                    public Object nextElement() {
                        return it.next().getKey();
                    }
                };
            }

            @Override
            public String[] getParameterValues(String s) {
                List<String> list = params.get(s);
                if(list==null) return null;
                return list.toArray(new String[list.size()]);
            }
        };
    }

    public HttpServletResponse getResponse() {
        return ctx.getResponse();
    }
}
